package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.dao.bean.Sick;
import com.qilu.pe.dao.bean.SickType;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 疾病分类
public class SickTypeActivity extends Base2Activity {
    private RecyclerView rcv_sick_type;// 疾病类型列表

    private SickTypeListAdapter adapter;
    private List<SickType> typeList = new ArrayList<>();

    private List<Sick> sickList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_sick_type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        rcv_sick_type = findViewById(R.id.rcv_sick_type);

        setHeadTitle("系统疾病分类");

        adapter = new SickTypeListAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_sick_type.setLayoutManager(manager);
        rcv_sick_type.setAdapter(adapter);

        setListener();

        hideProgress();
        reqSickType();
        showProgress();
    }

    private void setListener() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                for (int i = 0; i < typeList.size(); i++) {
                    if (i == position) {
                        typeList.get(i).setSelect(true);
                    } else {
                        typeList.get(i).setSelect(false);
                    }
                }

                sickList.clear();
                adapter.notifyDataSetChanged();
                hideProgress();
                reqSickList(typeList.get(position).getId());
                showProgress();
            }
        });
    }

    private Disposable reqSickType() {
        return APIRetrofit.getDefault().reqSickTypeList("he", "he")
                .compose(RxUtil.<BaseResult2<List<SickType>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<SickType>>>() {
                    @Override
                    public void accept(BaseResult2<List<SickType>> listResult2) throws Exception {
                        hideProgress();
                        if (listResult2.isSuccess()) {

                        } else {

                        }
                        typeList = listResult2.getData();
                        adapter.setDataList(typeList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private Disposable reqSickList(String type) {
        return APIRetrofit.getLs().reqSickList(type)
                .compose(RxUtil.<BaseResult2<List<Sick>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Sick>>>() {
                    @Override
                    public void accept(BaseResult2<List<Sick>> listResult2) throws Exception {
                        hideProgress();
                        if (listResult2.isSuccess()) {

                        } else {

                        }
                        sickList = listResult2.getData();
                        adapter.notifyDataSetChanged();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class SickTypeListAdapter extends ListBaseAdapter<SickType> {

        public SickTypeListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sick_type_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);
            ImageView iv_arrow = holder.getView(R.id.iv_arrow);
            RecyclerView rcv_sick = holder.getView(R.id.rcv_sick);

            SickType type = mDataList.get(position);
            tv_name.setText(type.getName());
            if (type.isSelect()) {
                iv_arrow.setImageResource(R.mipmap.ai_sick_down_arrow);

                rcv_sick.setVisibility(View.VISIBLE);
                SickListAdapter adapter = new SickListAdapter(SickTypeActivity.this);
                GridLayoutManager manager = new GridLayoutManager(SickTypeActivity.this, 3);
                rcv_sick.setLayoutManager(manager);
                rcv_sick.setAdapter(adapter);
                adapter.setDataList(sickList);

                // 点击具体的疾病
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View itemView, int position) {
//                        ToastUtils.showShort(sickList.get(position).getName());
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("sick", sickList.get(position));
                        startActivity(SickDetailActivity.class, bundle);
                    }
                });
            } else {
                iv_arrow.setImageResource(R.mipmap.ai_sick_right_arrow);
                rcv_sick.setVisibility(View.GONE);
            }
        }
    }

    private class SickListAdapter extends ListBaseAdapter<Sick> {

        public SickListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sick_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_name = holder.getView(R.id.tv_name);

            Sick sick = mDataList.get(position);
            tv_name.setText(sick.getName());
        }
    }
}